package com.pig4cloud.pig.iotData.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.iotData.entity.IotDataKgnpkDevDataInfoEntity;
import com.pig4cloud.pig.iotData.service.IotDataKgnpkDevDataInfoService;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * kg研究的检测土壤NPK检测的某时刻对应的详细数据存储
 *
 * @author lty
 * @date 2024-11-18 22:38:33
 */
@RestController
@RequiredArgsConstructor
//@PreAuthorize("@pms.hasPermission('iot_iotDevice_view')" ) todo 对于任何的设备数据查看 只要由iotdevice设备查看权限就好，不用单分权限 先不加
@RequestMapping("/duorouKgnpkDevDataInfo" )
@Tag(description = "duorouKgnpkDevDataInfo" , name = "kg研究的检测土壤NPK检测的某时刻对应的详细数据存储管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class IotDataKgnpkDevDataInfoController {

    private final IotDataKgnpkDevDataInfoService iotDataKgnpkDevDataInfoService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param duorouKgnpkDevDataInfo kg研究的检测土壤NPK检测的某时刻对应的详细数据存储
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    public R getDuorouKgnpkDevDataInfoPage(@ParameterObject Page page, @ParameterObject IotDataKgnpkDevDataInfoEntity duorouKgnpkDevDataInfo) {
		if(Objects.isNull(duorouKgnpkDevDataInfo.getKgnpkDevDataId())){
			page.setTotal(0);
			page.setPages(0);
			page.setRecords(new ArrayList());
			return R.ok(page);
		}

        LambdaQueryWrapper<IotDataKgnpkDevDataInfoEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(Objects.nonNull(duorouKgnpkDevDataInfo.getKgnpkDevDataId()), IotDataKgnpkDevDataInfoEntity::getKgnpkDevDataId,duorouKgnpkDevDataInfo.getKgnpkDevDataId());
		if(Objects.isNull(duorouKgnpkDevDataInfo.getKgnpkDevDataId())){

		}
        return R.ok(iotDataKgnpkDevDataInfoService.page(page, wrapper));
    }


    /**
     * 通过id查询kg研究的检测土壤NPK检测的某时刻对应的详细数据存储
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(iotDataKgnpkDevDataInfoService.getById(id));
    }

    /**
     * 新增kg研究的检测土壤NPK检测的某时刻对应的详细数据存储
     * @param duorouKgnpkDevDataInfo kg研究的检测土壤NPK检测的某时刻对应的详细数据存储
     * @return R
     */
    @Operation(summary = "新增kg研究的检测土壤NPK检测的某时刻对应的详细数据存储" , description = "新增kg研究的检测土壤NPK检测的某时刻对应的详细数据存储" )
    @SysLog("新增kg研究的检测土壤NPK检测的某时刻对应的详细数据存储" )
    @PostMapping
    public R save(@RequestBody IotDataKgnpkDevDataInfoEntity duorouKgnpkDevDataInfo) {
        return R.ok(iotDataKgnpkDevDataInfoService.save(duorouKgnpkDevDataInfo));
    }

    /**
     * 修改kg研究的检测土壤NPK检测的某时刻对应的详细数据存储
     * @param duorouKgnpkDevDataInfo kg研究的检测土壤NPK检测的某时刻对应的详细数据存储
     * @return R
     */
    @Operation(summary = "修改kg研究的检测土壤NPK检测的某时刻对应的详细数据存储" , description = "修改kg研究的检测土壤NPK检测的某时刻对应的详细数据存储" )
    @SysLog("修改kg研究的检测土壤NPK检测的某时刻对应的详细数据存储" )
    @PutMapping
    public R updateById(@RequestBody IotDataKgnpkDevDataInfoEntity duorouKgnpkDevDataInfo) {
        return R.ok(iotDataKgnpkDevDataInfoService.updateById(duorouKgnpkDevDataInfo));
    }

    /**
     * 通过id删除kg研究的检测土壤NPK检测的某时刻对应的详细数据存储
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除kg研究的检测土壤NPK检测的某时刻对应的详细数据存储" , description = "通过id删除kg研究的检测土壤NPK检测的某时刻对应的详细数据存储" )
    @SysLog("通过id删除kg研究的检测土壤NPK检测的某时刻对应的详细数据存储" )
    @DeleteMapping
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(iotDataKgnpkDevDataInfoService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param duorouKgnpkDevDataInfo 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    public List<IotDataKgnpkDevDataInfoEntity> export(IotDataKgnpkDevDataInfoEntity duorouKgnpkDevDataInfo, Long[] ids) {
        return iotDataKgnpkDevDataInfoService.list(Wrappers.lambdaQuery(duorouKgnpkDevDataInfo).in(ArrayUtil.isNotEmpty(ids), IotDataKgnpkDevDataInfoEntity::getId, ids));
    }
}